//
//  FirstCenterViewCell.m
//  SinaWebo
//
//  Created by 亦心 on 16/7/18.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "FirstCenterViewCell.h"
#import "FirstCenterView.h"
#import "LiveingViewController.h"
#import "Element.h"

@interface FirstCenterViewCell ()
@property (nonatomic, strong) NSMutableArray *viewArray;
@end

@implementation FirstCenterViewCell

- (NSMutableArray *)viewArray{
    if (_viewArray == nil) {
        _viewArray = [NSMutableArray array];
    }
    return _viewArray;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initView];
    }
    return self;
}

- (void)initView{
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor clearColor];
    for (NSInteger index = 0; index < 4; index++) {
        FirstCenterView *view = [FirstCenterView firstCenterView];
        view.tag = index;
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapView:)];
        [view addGestureRecognizer:tapGR];
        [self.contentView addSubview:view];
        [self.viewArray addObject:view];
    }

}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat vMargin = 10;
    CGFloat hMargin = 10;
    CGFloat vPading = 10;
    CGFloat hPading = 5;
    CGFloat viewW = (kScreenW - vPading - vMargin * 2) * 0.5;
    CGFloat viewH = (self.height - hPading - hMargin * 2) * 0.5;
    for (NSInteger index = 0; index < _dataArray.count; index++) {
        FirstCenterView *view = _viewArray[index];
        view.width = viewW;
        view.height = viewH;
        view.x = vMargin + (vPading + viewW) * (index % 2);
        view.y = hMargin + (hPading + viewH) * (index / 2);
    }
}


- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    for (NSInteger index = 0; index < _dataArray.count; index++) {
        FirstCenterView *view = _viewArray[index];
        view.element = _dataArray[index];
    }
}

- (void)onTapView:(UITapGestureRecognizer *)tapGesture{
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    LiveingViewController *liveVC = [[LiveingViewController alloc] init];
    Element *element = _dataArray[tapGesture.view.tag];
    liveVC.blog = element.mblog;
    [rootVC presentViewController:liveVC animated:YES completion:nil];
   
}


@end
